package taobao

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"sort"
	"strings"
	"time"
)

const (
	API       = "https://gw.api.taobao.com/router/rest"
	AppKey    = "34038331"
	AppSecret = "c7a77bd4b131c81491548714c5779476"
)

// 密钥
const secret = ""

type TaobaoHelper struct{}

//
//  createSign
//  @Description: 生成签名
//  @param data
//  @return string
//
func createSign(data map[string]interface{}) string {

	data["app_key"] = API
	data["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
	data["v"] = "2.0"
	data["sign_method"] = "md5"
	var sortData = make([]string, len(data))
	for key, value := range data {
		sortData = append(sortData, key+":"+value.(string))
	}
	sort.Strings(sortData)                        // 排序
	str := AppSecret + strings.Join(sortData, "") // 拼接
	h := md5.New()                                //md5
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}

const TPWD_CREATED = "taobao.tbk.tpwd.create"

//
//  GetTaoPasswd
//  @Description: 淘口令
//
func GetTaoPasswd(data map[string]interface{}) string {

	data["method"] = TPWD_CREATED
	data["sign"] = createSign(data)
	fmt.Println(data)
	return ""
}
